package com.order_system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.order_system.constants.Code;
import com.order_system.entity.Product;
import com.order_system.entity.ResModel;
import com.order_system.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李阳
 * @since 2023-01-06
 */
@RestController
@RequestMapping("/api/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 查询商品列表
     * @param product 商品类型
     * @return
     */
    @GetMapping("/list")
    public ResModel list(Product product){
        return productService.getList(product);
    }

    /**
     * 查询单个商品
     * @param productId 商品id
     * @return
     */
    @GetMapping("/{productId}")
    public Product list(@PathVariable Integer productId){
        return productService.selectById(productId);
    }

    /**
     * 删除商品
     * @param productId 商品id
     * @return
     */
    @DeleteMapping("/{productId}")
    public ResModel delete(@PathVariable Integer productId){
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(true,Product::getProductId,productId);
        return ResModel.success(Code.SUCCESS,productService.remove(wrapper));
    }

    /**
     * 修改商品
     * @param product 商品
     * @return
     */
    @PutMapping(value = "/update",produces = "application/json")
    public ResModel delete(@RequestBody Product product){
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(true,Product::getProductId,product.getProductId());
        return ResModel.success(Code.SUCCESS,productService.update(product,wrapper));
    }

    /**
     * 新增商品
     * @param product 商品
     * @return
     */
    @PostMapping(value = "/save",produces = "application/json")
    public ResModel save(@RequestBody Product product){
        return ResModel.success(Code.SUCCESS,productService.save(product));
    }

}

